package com.chase.framerwork.convertor;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.chase.framerwork.bean.Pager;
import com.chase.framerwork.util.CollectionUtil;

/**
 * Bean转换器,Bean和Vo相互转换
 * 
 * @Author Chase
 * @Date 2013-10-13
 * @param <E>
 * @param <V>
 */
@SuppressWarnings("unchecked")
public class BeanConvertor<E, V> implements Convertible<E, V>
{

	private Class<E> entityClass;
	private Class<V> voClass;

	public BeanConvertor(Class<E> entityClass, Class<V> voClass)
	{
		this.entityClass = entityClass;
		this.voClass = voClass;
	}

	@Override
	public E vo2Entity(V vo)
	{
		try
		{
			E entity = (E) entityClass.newInstance();

			BeanUtils.copyProperties(entity, vo);

			return entity;

		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public V entity2Vo(E entity)
	{

		try
		{
			V vo = (V) voClass.newInstance();

			BeanUtils.copyProperties(vo, entity);

			return vo;

		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public List<E> vo2Entity(List<V> voList)
	{
		List<E> entityList = new ArrayList<E>();
		try
		{
			for (V vo : voList)
			{
				E entity = (E) entityClass.newInstance();

				BeanUtils.copyProperties(entity, vo);

				entityList.add(entity);
			}
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return entityList;
	}

	@Override
	public List<V> entity2Vo(List<E> entityList)
	{

		List<V> voList = new ArrayList<V>();
		try
		{
			for (E entity : entityList)
			{
				V vo = (V) voClass.newInstance();

				BeanUtils.copyProperties(vo, entity);

				voList.add(vo);
			}
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return voList;
	}

	@Override
	public E[] Vo2Entity(V[] vos)
	{

		List<E> entityList = new ArrayList<E>();

		try
		{
			for (V vo : vos)
			{
				E entity = (E) entityClass.newInstance();

				BeanUtils.copyProperties(entity, vo);

				entityList.add(entity);
			}
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return (E[]) entityList.toArray();
	}

	@Override
	public V[] entity2Vo(E[] entitys)
	{

		List<V> voList = new ArrayList<V>();

		try
		{
			for (E entity : entitys)
			{
				V vo = (V) voClass.newInstance();

				BeanUtils.copyProperties(vo, entity);

				voList.add(vo);
			}
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			e.printStackTrace();
		}

		return (V[]) voList.toArray();
	}

	@Override
	public Pager convertorPager(Pager pager)
	{

		if (pager != null && CollectionUtil.isNotEmpty(pager.getResult()))
		{
			List<E> entityList = (List<E>) pager.getResult();

			List<V> voList = entity2Vo(entityList);

			pager.setResult(voList);
		}

		return pager;
	}

}
